package com.springboot.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.mapper.BVirtualWalletMapper;
import com.springboot.mapper.CartMapper;
import com.springboot.mapper.OrderDetailetMapper;
import com.springboot.mapper.OrdersMapper;
import com.springboot.mapper.UserMapper;
import com.springboot.mapper.VirtualWalletMapper;
import com.springboot.model.po.Cart;
import com.springboot.model.po.Credit;
import com.springboot.model.po.OrderDetailet;
import com.springboot.model.po.Orders;
import com.springboot.service.CreditService;
import com.springboot.service.OrdersService;
import com.springboot.service.VirtualWalletService;

import util.CalculateMemLevel;
import util.CommonUtil;

@Service
public class OrdersServiceImpl implements OrdersService{
	@Autowired
	private CartMapper cartMapper;
	
	@Autowired
	private OrdersMapper ordersMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private OrderDetailetMapper orderDetailetMapper;
	
	@Autowired
	private CreditService creditService;
	
	@Autowired
	private VirtualWalletService virtualWalletService;
	
	@Autowired
	private BVirtualWalletMapper bVirtualWalletMapper;
	
	@Autowired
	private VirtualWalletMapper virtualWalletMapper;
	
	@Override
	@Transactional
	//需要进行事务管理
	public int createOrders(Orders orders) {
		//1.查询用户当前购物车这个商家的所有食品
		Cart cart = new Cart();
		cart.setBusinessId(orders.getBusinessId());
		cart.setUserId(orders.getUserId());
		List<Cart> cartList = cartMapper.listCart(cart);
		
		//2.创建订单，返回生成的订单编号
		orders.setOrderDate(CommonUtil.getCurrentDate());//util里面
		ordersMapper.saveOrders(orders);//不获取返回值
		//系统日期自行添加
		int orderId = orders.getOrderId();//要的是订单编号而不是修改行数
		
		//3.批量添加订单明细
		List<OrderDetailet> list = new ArrayList();
		for(Cart c : cartList) {
			OrderDetailet od = new OrderDetailet();
			od.setFoodId(c.getFoodId());
			od.setOrderId(orderId);
			od.setQuantity(c.getQuantity());
			orderDetailetMapper.saveOrderDetailetBatch(orderId,c.getFoodId(),c.getQuantity());
		}
		
		
		//4.删除购物车中相关信息
		cartMapper.removeCart(cart);//userId和businessId都设置好
		
		
		return orderId;
	}

	@Override
	public Orders getOrdersById(Integer orderId) {
		return ordersMapper.getOrdersById(orderId);
	}

	@Override
	public List<Orders> listOrdersByUserId(String userId) {
		return ordersMapper.listOrdersByUserId(userId);
	}

	@Override
	@Transactional//这里涉及多个dao操作需要事务开启
	public int updateOrderState(Integer orderId, Integer orderState , BigDecimal amount) {
		//添加功能
		Orders orders = ordersMapper.getOrdersById(orderId);//获取当前订单
		if(orders == null) {
			return 0;
		}
		if(orderState == 1 || orderState == 0) {//满足规范的state才能改变
			if(ordersMapper.getOrdersById(orderId).getOrderState() == 0 && orderState == 1) {
				
				//2.更新积分
				BigDecimal oldPoints = userMapper.getPoints(orders.getUserId());
				//    积分明细更新
				Credit credit = new Credit();
				credit.setChannelType(0);//下单返利
				credit.setNum(orders.getOrderTotal());//这里的积分数是正数
				credit.setUserId(orders.getUserId());
				creditService.insertCredit(credit);//包含了新的积分和
				
				BigDecimal points = oldPoints.add(orders.getOrderTotal());//计算新积分
				BigDecimal maxPoints = userMapper.getMaxPoints(orders.getUserId());
				maxPoints = maxPoints.add(orders.getOrderTotal());
				//积分更新
				userMapper.updatePoints(orders.getUserId(), points);
				userMapper.updateMemLevel(orders.getUserId(), CalculateMemLevel.calculateMemLevel(maxPoints));
				//余额更新
				Integer fromWalletId = virtualWalletMapper.getVirtualWalletByUserId(orders.getUserId()).getWalletId();
				//端对端转账
				Integer toWalletId = bVirtualWalletMapper.getVirtualWalletByBusinessId(orders.getBusinessId()).getWalletId();
				int result1 = virtualWalletService.transfer(fromWalletId , toWalletId , amount, orderId);
				if(result1 ==1)
					return  ordersMapper.updateOrderState(orderId, orderState);
				else
					return 0;
			}else
				return 0;//其余不合法
		}//存在再进
		else {
			return 0;
		}
		
	}

	@Override
	public int setOrderTotal(Integer orderId, BigDecimal orderTotal) {
		return ordersMapper.setOrderTotal(orderId, orderTotal);
	}
	
	
}
